<?php

class Sondage_AuthentificationController extends Zend_Controller_Action
{

    public function init()
    {
    	
    	parent::init();
        /* Initialize action controller here */
    	$this->_helper->layout()->disableLayout();
    	$this->_helper->layout()->setLayout("authlayout");
    	
    }

    public function indexAction()
    {
        // action body
    	// action body
    	
    	$inscriptionform = new Application_Form_Inscription();
    	$this->view->form = $inscriptionform;
    	 $this->view->title="Register";
    	if ($this->_request->isPost()) {
    		$formData = $this->_request->getPost();
    		if ($inscriptionform->isValid($formData)) {
    			$formation = new Application_Model_DbTable_Sondagepersonne();
    			
    			
    			$datarow=$formation->fetchRow(" email = '".$inscriptionform->getValue('email')."'");
    			
    			if($datarow && is_array($datarow->toArray() )){
    				$this->_helper->redirector("login","authentification","sondage");
    			}
    			
    			$row = $formation->createRow();
    			 
    			$row->nom = $inscriptionform->getValue('nom');
    			$row->prenom = $inscriptionform->getValue('prenom');
    			//$row->adresse = $inscriptionform->getValue('adresse');
    			$row->email = $inscriptionform->getValue('email');
    			$row->login = $inscriptionform->getValue('login');
    			$row->password = $inscriptionform->getValue('password');
    			$row->telephone = $inscriptionform->getValue('telephone');
    			$row->CIN = $inscriptionform->getValue('CIN');
    			$row->ETATB = $inscriptionform->getValue('ETATB');
    			
    			
    			$row->role = 'participant';
    			 
    			$result = $row->save();
    			 
    			 
    			 
    			//	$inscriptionform->reset();
    			 
    			$this->_helper->redirector("index","monsondage","sondage");
    		}
    	}
    	 
    }

    public function loginAction()
    {
        // action body
    	$auth= new Application_Form_Authentification();
    	
    	if ($this->_request->isPost()) {
    		 
    		$formData = $this->_request->getPost();
    		 
    		if ($auth->isValid($formData)) {
    			$request  = $this->getRequest();
    			 
    			$auth = Zend_Auth::getInstance();
    			$bootstrap = $this->getInvokeArg('bootstrap');
    			$dbResource = $bootstrap->getPluginResource('db');
    			$dbAdapter = $dbResource->getDbAdapter();
    	
    			$authAdapter = new Zend_Auth_Adapter_DbTable($dbAdapter);
    			$authAdapter->setTableName('sondagepersonne')
    			->setIdentityColumn('email')
    			->setCredentialColumn('password');
    			// Set the input credential values
    			$uname = $request->getParam('email');
    			$paswd = $request->getParam('password');
    			$authAdapter->setIdentity($uname);
    			//$authAdapter->setCredential(md5($paswd));
    			$authAdapter->setCredential($paswd);
    			// Perform the authentication query, saving the result
    			$result = $auth->authenticate($authAdapter);
    	
    			if($result->isValid()){
    				 
    				$data = $authAdapter->getResultRowObject(null,'password');
    				$auth->getStorage()->write($data);
    	
    				if($identity->role=="admin"){
    					$this->_helper->redirector('sondage','gsondage','sondage');
    				}else{
    				 	
    					$this->_helper->redirector("index","monsondage","sondage");
    				}
    			}else{
    				 $this->_helper->redirector('login',"authentification","sondage");
    			}
    			 
    		}
    	}
    	$this->view->title="Authentification";
    	$this->view->form=$auth;
    }

    public function logoutAction()
    {
        // action body

    	$auth = Zend_Auth::getInstance();
    	$auth->clearIdentity();
    	$this->_helper->redirector("login","authentification","sondage");
    }

    public function getresultAction()
    {
        // action body
    	$id=$this->_request->getParam("id");
    	 
    	$mesgagnants=new Application_Model_Mapper_Lesgagnants();
    	$listgagnant=$mesgagnants->hasGaganant($id);
    	
    	$paginator = Zend_Paginator::factory($listgagnant);
    	 
    	//ajouter le nombre d'enregistrement par page
    	$paginator->setItemCountPerPage(5);
    	//ajouter la page par defaut
    	$paginator->setCurrentPageNumber($this->_getParam('page', 1));
    	//ajouter le paginateur au view
    	$this->view->paginator = $paginator;
    	
    	
    	$headercolums=array(
    			"nom"=> "Nom",
    			"prenom"=>"Prenom",
    			"email"=>"E-mail",
    			"titre"=>"Titre Sondage",
    			"scorefinal"=>"Score final",
    			"telephone"=>"téléphone"
    	
    	);
    	$this->view->headercolums=$headercolums;
    	
    	Zend_View_Helper_PaginationControl::setDefaultViewPartial('controls.phtml');
    	 
    	
    	if ($this->_request->isPost()) {
    		 
    		$this->_helper->redirector("gestionsondage");
    	
    	}
    }


}







